package com.huanletao.dom4j;


import com.huanletao.dom4j.pojo.Address;
import com.huanletao.dom4j.pojo.Employee;
import com.huanletao.dom4j.pojo.NetWorkInfo;
import com.huanletao.dom4j.pojo.Student;
import org.dom4j.DocumentException;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *
 * @package: com.huanletao.dom4j
 * @Description:
 * @className: demo
 * @author: @NNCSKJ ADMIN
 * @copyright: Copyright (c) 2022/7/30 11:45
 * @version: V1.0
 * @see XmlDocumentBuilderExample
 * XmlDocumentResolver 使用示例, 主要实现将xml文档内容,填充到javabean对象中,
 * 实现的功能依赖 javax.xml.bind.annotation.XmlElement 注解实现,
 *
 * </p>
 */

public class XmlDocumentBuilderExample {

    /**
     * 构建简单文档格式;
     */
    @Test
    public void buildSimpleDocument() throws DocumentException {
        Student student = getStudent();
        String document = DocumentBuilder.build(student, Student.class);
        System.out.println(document);
    }



    /**
     * xpath 查找指定位置,构建文档;
     */
    @Test
    public void buildDocumentAtXpath() throws DocumentException {
        Student student = getStudent();

        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<request>\n" +
                "  <header> </header>\n" +
                "  <body> </body>\n" +
                "</request>";
        //在body 下创建xml文档,
        String xpathExpresssion = "/request/body";
        String document = DocumentBuilder.build(student, Student.class, xmlStr, xpathExpresssion);
        System.out.println(document);
    }

    /**
     *
     * 泛型构建xml文档结构,
     * Employee class 中有一个T 的类型未指定,
     * Emp class 继承 Employee 去指定T 的类型是 Address 构建文档;
     * Emp1 class 继承 Employee 去指定T 的类型是 NetworkInfo
     * 构建时会构建不同T 的标签 内容;
     */
    @Test
    public void buildDocumentByGeneric() throws DocumentException {
        Employee<Address> employee = new Employee<Address>(){};
        employee.setDateOfBirth(new Date());
        employee.setSsn(1000011L);
        employee.setEmployeeType(1);
        employee.setName("tom");
        employee.setSalary(new BigDecimal("10000.00"));

        Address address = new Address();
        address.setCode(11088);
        address.setName("南宁工程创建基地");

        NetWorkInfo info = new NetWorkInfo();
        info.setHost("localhost");
        info.setPort("9001");
        employee.setBody(address);

        String xmlDocument = DocumentBuilder.build(employee,employee.getClass());

        System.out.println(xmlDocument);
    }

    private Student getStudent() {
        Student student = new Student();
        student.setAge(12);
        student.setGradeName("105班");
        student.setSex("男");
        student.setDateOfBirth(new Date());
        return student;
    }

    public static void main(String[] args) {
        //断言1结果为true，则继续往下执行
        assert true;
        System.out.println("断言1没有问题，Go！");
        System.out.println("\n-----------------\n");
        //断言2结果为false,程序终止

        assert false : "断言失败，此表达式的信息将会在抛出异常的时候输出！";
        System.out.println("断言2没有问题，Go！");
    }
}
